package com.itzyf.pokemondata.activity.illustration

import android.text.Html
import android.text.TextUtils
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.itzyf.pokemondata.R
import com.itzyf.pokemondata.bean.*
import com.itzyf.pokemondata.utils.OnRefreshListener
import com.itzyf.pokemondata.utils.setOnRefreshListener
import com.tydic.baselibrary.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_list.*
import org.litepal.LitePal
import java.util.*

class ExclusiveActivity : BaseActivity() {
    var mDatas: MutableList<ExclusiveDo?> = ArrayList()
    var index = 0
    private lateinit var mAdapter: QuickAdapter

    override fun handlerActivity() {
        mSearchView.hint = "请输入道具或精灵名称"

        mAdapter = QuickAdapter(R.layout.item_exclusive, mDatas)
        rv_list.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
        rv_list.adapter = mAdapter
//        showProgressDialog()
        queryDatas()

        mAdapter.setOnLoadMoreListener({ queryDatas(index++) }, rv_list)

        pull_to_refresh.setOnRefreshListener(object : OnRefreshListener {
            override fun onRefresh() {
                index = 0
                queryDatas(index)
            }
        })

        mBtnSearch.setOnClickListener {
            val query = mSearchView.text.toString()
            queryDatas(query = query)
        }
        mSearchView.setOnEditorActionListener(TextView.OnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                val query = mSearchView.text.toString()
                queryDatas(query = query)
                return@OnEditorActionListener true
            }
            false
        })
    }

    override fun getContentViewId() = R.layout.activity_list

    override fun getToolbarTitle() = "专属道具"


    /**
     * 查询数据
     */
    fun queryDatas(index: Int = 0, pageSize: Int = 20, query: String = "") {
        var where = ""
        if (!TextUtils.isEmpty(query)) {
            where = "pokemonName like '%$query%' or name  like '%$query%'"
        }
        val dataList =
            LitePal
                .select("name", "invalid", "pokemonName", "effect")
                .limit(pageSize)
                .offset(index * pageSize)
                .order("createdAt desc")
                .where(where)
                .find(DbExclusive::class.java)
        if (index == 0) {
            mDatas.clear()
            pull_to_refresh.finishRefresh()
        }

        for (obj in dataList) {
            val exclusive = ExclusiveDo()
            exclusive.effect = obj.effect
            exclusive.pokemonName = obj.pokemonName
            exclusive.invalid = obj.isInvalid
            exclusive.name = obj.name
            mDatas.add(exclusive)
        }

        if (dataList.size < pageSize) {
            mAdapter.loadMoreEnd()
        } else {
            mAdapter.loadMoreComplete()
        }
        mAdapter.notifyDataSetChanged()
    }


    internal inner class QuickAdapter(layoutResId: Int, data: List<ExclusiveDo?>) :
        BaseQuickAdapter<ExclusiveDo, BaseViewHolder>(layoutResId, data) {

        override fun convert(helper: BaseViewHolder, item: ExclusiveDo?) {
            helper.setText(R.id.tvName, item?.name)
            helper.setText(R.id.tvPokemonName, item?.pokemonName)
            val html = "${item?.effect}<font color=\"#FF0000\">无法被无效</font>"
            helper.setText(R.id.tvEffect, if (item!!.invalid) item.effect else Html.fromHtml(html))
        }
    }
}
